<template>
    <div>
        <el-form label-width="80px" style="width: 400px;" @submit.native.prevent :model="form" :rules="rules" ref="ruleForm">
            <el-form-item label="演员名字" prop="actorName">
                <el-input  placeholder="请输入演员名字" v-model="form.actorName"></el-input>
            </el-form-item>
            <el-form-item label="演员头像" prop="actorAvatar">
                <el-upload
                    class="avatar-uploader"
                    :action="`${$UPLOADURL}/upload`"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="form.actorAvatar" :src="form.actorAvatar" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="sumbitBtn">立即新增</el-button>
                <el-button>取消</el-button>
            </el-form-item>
        </el-form>

    </div>
</template>

<script>
    import httpApi from "@/http/index"; 
    export default {
        data() {
            return {
               form:{
                actorName:'',
                actorAvatar:''
               },
               rules:{
                actorName:[{required:true,message:'姓名不能为空',trigger:'blur'}],
                actorAvatar:[{required:true,message:'图片路径不能为空',trigger:'blur'}]
               }
            }
        },
            methods: {
            handleAvatarSuccess(res, file) {
                    console.log("上传成功后的响应", res,file);
                    // 将图片的访问链接赋值给 form.actorAvatar
                    this.form.actorAvatar = res.data;
                    // 移除错误验证消息
                    this.$refs['ruleForm'].clearValidate('actorAvatar')
                    // 移出验证错误消息
                    // this.$refs["form"].clearValidate("actorAvatar");
                // this.actorAvatar = URL.createObjectURL(file.raw);
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            sumbitBtn(){
                this.$refs['ruleForm'].validate((valid)=>{
                    if(valid){
                        // 表单验证通过发起的请求
                        // let url = 'http://localhost:3010/movie-actor/add'
                        // console.log(url)
                        // myaxios.post(url,this.form).then((res)=>{
                        //     console.log('提交表单请求结果',res)
                        //     if(res.data.code == 200){
                        //         this.$message({message:"演员新增成功",type:'success'})
                        //     }else{
                        //         this.$message({message:"演员新增失败",type:'error'})
                        //     }
                        //     // 重置表单
                        // this.$refs['ruleForm'].resetFields()
                        // })
                        httpApi.actorApi.addActor(this.form).then(res=>{
                            console.log('提交表单请求结果',res)
                            if(res.data.code == 200){
                                this.$message({message:"演员新增成功",type:'success'})
                            }else{
                                this.$message({message:"演员新增失败",type:'error'})
                            }
                            this.$refs['ruleForm'].resetFields()
                        })
                    }else{
                        console.log("表单验证失败，阻止表单提交");
                    }
                })
                
            }
            }
    }
</script>

<style lang="scss" scoped>
</style>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 130px;
    height: 130px;
    line-height: 130px;
    text-align: center;
  }
  .avatar {
    width: 130px;
    height: 130px;
    display: block;
    object-fit: cover;
  }
</style>